// function any(promises) {
//     return new Promise((resolve, reject) => {
//         const errors = [];
//         let rejectedCount = 0;

//         if (promises.length === 0) {
//             reject(new AggregateError([], 'All promises were rejected'));
//             return;
//         }

//         promises.forEach((promise, index) => {
//             Promise.resolve(promise).then(
//                 (value) => {
//                     resolve(value);
//                 },
//                 (error) => {
//                     errors[index] = error;
//                     rejectedCount++;
//                     if (rejectedCount === promises.length) {
//                         reject(new AggregateError(errors, 'All promises were rejected'));
//                     }
//                 }
//             );
//         });
//     });
// }

// const promises = [
//     new Promise((resolve, reject) => {
//         resolve(1);
//         reject('error1');
//     }),
//     new Promise((resolve, reject) => {
//         resolve(2);
//         reject('error2');
//     }),
//     new Promise((resolve, reject) => {
//         resolve(3);
//         reject('error3');
//     })
// ];

// any(promises)
//    .then((result) => {
//         console.log('Resolved:', result);
//     })
//    .catch((error) => {
//         console.error('Rejected:', error);
//     });
// new Promise((resolve,reject)=>{
//     resolve([1,2,3])
//     reject('error')
// }).then(val=>console.log(val)).catch(err=>console.log(err))
const testPromise = new Promise((resolve, reject) => {
    console.log(4);
  resolve(1);
})
  .then((value) => {
    console.log(value);
    // throw Error('test')
    return 2;
  })
  .then((value) => {
    console.log(value);
  })
  .catch((err) => {
    console.log(err);
  });
  console.log(34);
class MyPromise {
  sstatus = "pending";
  value = undefined;
  error = undefined;
  onFulfilledCallbacks = [];
  onRejectedCallbacks = [];

  constructor(executor) {
    const resolve = (value) => {
      if (this.status !== "pending") return;
      // 处理 thenable 对象
      const resolveValue = (v) => {
        this.status = "fulfilled";
        this.value = v;
        this.onFulfilledCallbacks.forEach((fn) => fn());
      };
      // 异步调度
      queueMicrotask(() => {
        if (value instanceof MyPromise) {
          value.then(resolveValue);
        } else {
          resolveValue(value);
        }
      });
    };

    const reject = (error) => {
      if (this.status !== "pending") return;
      // 异步调度
      queueMicrotask(() => {
        this.status = "rejected";
        this.error = error;
        this.onRejectedCallbacks.forEach((fn) => fn());
      });
    };

    try {
      executor(resolve, reject);
    } catch (error) {
      reject(error);
    }
  }
  then(onFulfilled, onRejected) {
    return new MyPromise((resolve, reject) => {
      const handle = () => {
        try {
          const result =
            this.status === "fulfilled"
              ? onFulfilled(this.value)
              : onRejected(this.error);
          resolve(result);
        } catch (error) {
          reject(error);
        }
      };

      if (this.status === "pending") {
        this.onFulfilledCallbacks.push(handle);
        this.onRejectedCallbacks.push(handle);
      } else {
        queueMicrotask(handle);
      }
    });
  }
  catch(errorCallBack) {
    return new MyPromise(() => {
      errorCallBack(this.error);
    });
  }
  static all(promises) {
    return new MyPromise((resolve, reject) => {
      const results = [];
      let count = 0;
      promises.forEach((promise, index) => {
        MyPromise.resolve(promise).then(
          (value) => {
            results[index] = value;
            if (++count === promises.length) resolve(results);
          },
          (error) => reject(error)
        );
      });
    });
  }
  static allSettled(promises) {
    return new MyPromise(() => {
      const result = [];
      promises.forEach((promise) => {
        try {
          result.push(this.resolve(promise));
        } catch (error) {
          result.push(this.reject(error));
        }
      });
      this.resolveMethod(result);
    });
  }
  static any(promises) {
    return new MyPromise(() => {
      const errors = [];
      promises.forEach((promise) => {
        try {
          this.resolveMethod(promise);
        } catch (error) {
          errors.push(this.reject(error));
        }
      });
      if (errors.length === promises.length) this.rejectMethod(errors);
    });
  }
  static race(promises) {
    return new MyPromise(() => {
      try {
        this.resolveMethod(promises[0]);
      } catch (error) {
        this.rejectMethod(error);
      }
    });
  }
  static resolve(value) {
    return new MyPromise((resolve) => resolve(value));
  }

  static reject(error) {
    return new MyPromise((_, reject) => reject(error));
  }
}
